<template>
  <div class="login">
    <div class="login-content">
      <div class="login-form">
        <div class="logo_box">
          <img src="../assets/images/login/logo.png" alt="">
        </div>
        <div class="title">
          <p>全科+社区居家医养结合智慧管理平台</p>
          <p>General Practice based Intelligent Management Platform for Home-dwelling Elderly</p>
        </div>

        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="username" class="input_box">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password" class="input_box">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
          <el-form-item class="check_box">
            <el-checkbox v-model="loginForm.rememberMe">
              自动登录
            </el-checkbox>
          </el-form-item>
          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleLogin"
              class="login-btn"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.data.img;
          this.loginForm.uuid = res.data.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  background-color: #1890FF;
  background: url(../assets/images/login/login_bg.png) no-repeat center;
  background-size: cover;
}

.login-content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;

  .login-form {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .logo_box {
      width: 161px;
      height: 161px;
      margin-bottom: 39px;
      img {
        width: 100%;
      }
    }
    .title {
      text-align: center;
      margin-bottom: 50px;
      p {
        margin: 0;
        color: #07151E;
      }
      p:nth-child(1) {
        font-size: 24px;
        font-weight: bold;
        line-height: 40px;
      }
      p:nth-chid(2) {
        font-size: 12px;
      }
    }
    .input_box {
      width: 362px;
      height: 56px;
      position: relative;
      margin-bottom: 25px;
      line-height: 56px;
      .el-input__prefix {
        position: absolute;
        height: 29px;
        width: 64px;
        left: 0;
        top: 14px;
        border-right: 1px solid #DDDDDD;
      }
      .input-icon {
        font-size: 20px;
      }
    }
    .check_box {
      margin: 20px 0 40px;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      .el-checkbox__label {
        color: #ACACAC;
      }
    }

    .el-input--medium .el-input__inner {
      height: 56px;
      line-height: 56px;
      border-radius: 28px;
      font-size: 16px;
      padding-left: 83px;
    }

    .el-form-item__error {
      padding-top: 10px;
      left: 15px;
    }

    .login-btn {
      width: 362px;
      height: 56px;
      background-color: #1890FF;
      border-color: #1890FF;
      border-radius: 28px;
      color: #fff;
      font-size: 18px;
    }

    .el-checkbox__input.is-checked .el-checkbox__inner {
      background-color: #1890FF;
      border-color: #1890FF;
    }

    .el-checkbox__inner {

      &:hover {
        border-color: #1890FF;
      }
    }

    .el-checkbox:last-of-type {
      margin-right: 10px;
    }
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.login-code-img {
  height: 38px;
}
</style>
